(function (angular) {
    'use strict';

    var mi = angular.module('ytong',
        [
            'ytong.controllers', 'ytong.services','ytong.directives','ui.router','angular-growl','angularModalService', 'ui.bootstrap'
        ]
    );

    angular.module('ytong.controllers',[]);
    angular.module('ytong.services',[]);
    angular.module('ytong.directives',[]);


    mi.config(config);
    mi.run(run);

    function config($stateProvider, Constants, growlProvider) {

        growlProvider.globalTimeToLive(3000);
        growlProvider.globalDisableCountDown(true);

        $stateProvider
            .state(Constants.STATES.ANONYMOUS, {
                abstract: true,
                template: "<ui-view>"
            })
            .state(Constants.STATES.LOGIN, {
                url: '',
                templateUrl: 'src/login/login.html',
                controller: 'loginCtrl as lc'
            });

        $stateProvider
            .state(Constants.STATES.ADMIN.ADMIN, {
                abstract: true,
                templateUrl: 'src/admin/admin.html',
                controller: 'adminCtrl as mc'
            })
            .state(Constants.STATES.ADMIN.DASHBOARD, {
                views: {
                    'body': {
                        templateUrl: 'src/admin/dashboard/dashboard.html',
                        controller: 'adminDashCtrl as dc'
                    }
                }
            })
            .state(Constants.STATES.ADMIN.AUTHORIZE, {
                views: {
                    'body': {
                        templateUrl: 'src/admin/authorize/authorize.html',
                        controller: 'authorizeCtrl as ac'
                    }
                }
            })
            .state(Constants.STATES.ADMIN.USERS, {
                views: {
                    'body': {
                        templateUrl: 'src/admin/users/users.html',
                        controller: 'usersCtrl as uc'
                    }
                }
            })
            .state(Constants.STATES.ADMIN.FEEDBACKS, {
                views: {
                    'body': {
                        templateUrl: 'src/admin/feedback/feedback.html',
                        controller: 'feedbackCtrl as fc'
                    }
                }
            })
            .state(Constants.STATES.ADMIN.MESSAGES, {
                views: {
                    'body': {
                        templateUrl: 'src/messages/messages.html',
                        controller: 'messagesCtrl as msgc'
                    }
                }
            });

        $stateProvider
            .state(Constants.STATES.MANAGER.MANAGER, {
                abstract: true,
                templateUrl: 'src/manager/manager.html',
                controller: 'managerCtrl as mc'
            })
            .state(Constants.STATES.MANAGER.DASHBOARD, {
                views: {
                    'body': {
                        templateUrl: 'src/manager/dashboard/dashboard.html',
                        controller: 'managerDashCtrl as dc'
                    }
                }
            })
            .state(Constants.STATES.MANAGER.MESSAGES, {
                views: {
                    'body': {
                        templateUrl: 'src/messages/messages.html',
                        controller: 'messagesCtrl as msgc'
                    }
                }
            })
            .state(Constants.STATES.MANAGER.ORDERS, {
                views: {
                    'body': {
                        templateUrl: 'src/orders/orders.html',
                        controller: 'ordersCtrl as oc'
                    }
                }
            })
            .state(Constants.STATES.MANAGER.ORDER, {
                views: {
                    'body': {
                        templateUrl: 'src/orders/order.html',
                        controller: 'orderCtrl as oc'
                    }
                }
            })
            .state(Constants.STATES.MANAGER.RESOURCES_DASH, {
                views: {
                    'body': {
                        templateUrl: 'src/manager/resources/dashboard.html',
                        controller: 'resourcesDashCtrl as rdc'
                    }
                }
            })
            .state(Constants.STATES.MANAGER.RESOURCES.TRUCKS, {
                views: {
                    'body': {
                        templateUrl: 'src/manager/resources/trucks.html',
                        controller: 'resourcesCtrl as rsc'
                    }
                }
            })
            .state(Constants.STATES.MANAGER.RESOURCES.CRANES, {
                views: {
                    'body': {
                        templateUrl: 'src/manager/resources/cranes.html',
                        controller: 'resourcesCtrl as rsc'
                    }
                }
            })
            .state(Constants.STATES.MANAGER.RESOURCES.PRODUCTS, {
                views: {
                    'body': {
                        templateUrl: 'src/manager/resources/products.html',
                        controller: 'resourcesCtrl as rsc'
                    }
                }
            })
            .state(Constants.STATES.MANAGER.RESOURCES.AREAS, {
                views: {
                    'body': {
                        templateUrl: 'src/manager/resources/areas.html',
                        controller: 'resourcesCtrl as rsc'
                    }
                }
            })
            .state(Constants.STATES.MANAGER.REPORTS_DASH, {
                views: {
                    'body': {
                        templateUrl: 'src/manager/reports/dashboard.html',
                        controller: 'reportsDashCtrl as rpdc'
                    }
                }
            })
            .state(Constants.STATES.MANAGER.REPORTS.ORDERS, {
                views: {
                    'body': {
                        templateUrl: 'src/manager/reports/reports.html',
                        controller: 'reportsCtrl as rpc',
                        resolve: {
                            reportType: function() {
                                return Constants.REPORTS.ORDERS;
                            }
                        }
                    }
                }
            })
            .state(Constants.STATES.MANAGER.REPORTS.LATES, {
                views: {
                    'body': {
                        templateUrl: 'src/manager/reports/reports.html',
                        controller: 'reportsCtrl as rpc',
                        resolve: {
                            reportType: function() {
                                return Constants.REPORTS.LATE;
                            }
                        }
                    }
                }
            });

        $stateProvider
            .state(Constants.STATES.SALESMAN.SALESMAN, {
                abstract: true,
                templateUrl: 'src/salesman/salesman.html',
                controller: 'salesmanCtrl as mc'
            })
            .state(Constants.STATES.SALESMAN.DASHBOARD, {
                views: {
                    'body': {
                        templateUrl: 'src/salesman/dashboard/dashboard.html',
                        controller: 'salesmanDashCtrl as dc'
                    }
                }
            })
            .state(Constants.STATES.SALESMAN.MESSAGES, {
                views: {
                    'body': {
                        templateUrl: 'src/messages/messages.html',
                        controller: 'messagesCtrl as msgc'
                    }
                }
            })
            .state(Constants.STATES.SALESMAN.CUSTOMERS, {
                views: {
                    'body': {
                        templateUrl: 'src/salesman/customers/customers.html',
                        controller: 'customersCtrl as cc'
                    }
                }
            })
            .state(Constants.STATES.SALESMAN.ORDERS, {
                views: {
                    'body': {
                        templateUrl: 'src/orders/orders.html',
                        controller: 'ordersCtrl as oc'
                    }
                }
            })
            .state(Constants.STATES.SALESMAN.ORDER, {
                views: {
                    'body': {
                        templateUrl: 'src/orders/order.html',
                        controller: 'orderCtrl as oc'
                    }
                }
            });
    }

    function run($rootScope, authenticationService,  Constants, authorizationManager, feedbacksManager, usersManager, customerManager,
                 resourceManager, ordersManager){

        $rootScope.$on('$stateChangeStart',
            function(event, toState, toParams, fromState, fromParams){
                if(toState.name === Constants.STATES.LOGIN){
                    return;
                }

                $rootScope.$broadcast('promisesChangedEvent', {
                    isLoading: true
                });

                feedbacksManager.loadMessages();
                usersManager.loadUsers();

                switch(authenticationService.getCurrentUserRole()){
                    case '0':{
                        authorizationManager.loadUnauthorizedUsers();
                        feedbacksManager.loadFeedbacks();
                        feedbacksManager.loadDelayedFeedbacks();
                        break;
                    }
                    case '1':{
                        resourceManager.loadResource(Constants.URLS.TRUCKS_URL, Constants.RESOURCES.TRUCKS);
                        resourceManager.loadResource(Constants.URLS.CRANES_URL, Constants.RESOURCES.CRANES);
                        resourceManager.loadResource(Constants.URLS.PRODUCTS_URL, Constants.RESOURCES.PRODUCTS);
                        resourceManager.loadResource(Constants.URLS.AREAS_URL, Constants.RESOURCES.AREAS);
                        ordersManager.loadOrders();
                        ordersManager.loadDashboardOrders();
                        customerManager.loadCustomers();
                        break;
                    }
                    case '2':{
                        resourceManager.loadResource(Constants.URLS.PRODUCTS_URL, Constants.RESOURCES.PRODUCTS);
                        customerManager.loadCustomers();
                        ordersManager.loadOrders();
                        ordersManager.loadDashboardOrders();
                        break;
                    }

                };

            });
    };


    }(angular));